<template>
  <div class="app-container calendar-list-container user-container">
    <div class="filter-container">
      <el-form>
        <el-row :gutter="0" class="cutOffLine" v-show="typeList.length !== 0">
          <el-form-item label="类别">
            <template>
              <el-select v-model="currentType"
                         clearable
                         placeholder="请选择"
                         @change="onChange">
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-form-item>
        </el-row>
        <el-row :gutter="0" class="cutOffLine">
          <el-col :span="18">
            <el-button @click="addBanner" size="small" type="primary">
              <icon name="upload"/>
              新增
            </el-button>
            <el-button
              :disabled="currentRow && !currentRow.id"
              @click="deleteBanner"
              size="small"
              type="primary"
            >
              <icon name="trash"/>
              删除
            </el-button>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <el-button @click="queryBannerList" size="small" type="primary">
              <icon name="refresh"/>
              刷新
            </el-button>
            <el-button @click="preview = !preview" size="small" type="primary">
              <icon name="television"/>
              {{preview ? '预览: 开' : '预览: 关'}}
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-carousel :interval="4000" height="400px" type="card" v-if="preview">
      <el-carousel-item :key="item.id" v-for="item in bannerList" v-if="item.show === '1'">
        <el-image :src="item.url" fit="scale-down" style="width: 600px; height: 300px"/>
      </el-carousel-item>
    </el-carousel>
    <el-scrollbar>
      <el-table
        :data="bannerList"
        @current-change="handleCurrentChange"
        @row-dblclick="handleDoubleSelect"
        border
        element-loading-background="rgba(0, 0, 0, 0.8)"
        element-loading-spinner="el-icon-loading"
        element-loading-text="拼命加载中"
        highlight-current-row
        ref="bannerTable"
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column align="center" label="顺序" prop="displayOrder" width="50px"/>
        <el-table-column align="center" label="详情" width="450px">
          <template slot-scope="scope">
            <el-image :src="scope.row.url" fit="fill" style="width: 400px; height: 200px"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="跳转地址" prop="redirectUrl" width="300px"/>
        <el-table-column align="center" label="显示" width="80px">
          <template slot-scope="scope">
            <div>{{getDisplay(scope.row.show)}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" prop="createAt" width="100px"/>
        <el-table-column align="center" label="修改时间" prop="updateAt" width="100px"/>
      </el-table>
    </el-scrollbar>
    <el-dialog
      :title="updateDialog.add ? '新增' : '修改' + updateDialog.title"
      :visible.sync="updateDialog.visible"
      @close="updateDialog.visible = false"
      @open="setUpdateDialogData"
      width="60%"
    >
      <el-form>
        <el-form-item label="是否显示">
          <el-switch active-value="1" inactive-value="0" v-model="updateDialog.data.show"/>
        </el-form-item>
        <el-form-item label="顺序">
          <el-input-number label="显示顺序" size="mini" v-model="updateDialog.data.displayOrder"/>
        </el-form-item>
        <el-form-item label="上传图片">
          <el-upload
            :accept="'image/*'"
            :http-request="uploadCos"
            :on-success="handleUploadSuccess"
            :show-file-list="false"
            action
            class="avatar-uploader"
            list-type="picture"
          >
            <img :src="updateDialog.data.url" class="avatar" v-if="updateDialog.data.url"/>
            <i class="el-icon-plus avatar-uploader-icon" v-else/>
          </el-upload>
        </el-form-item>
        <el-form-item label="跳转地址">
          <el-input v-model="updateDialog.data.redirectUrl"/>
        </el-form-item>
      </el-form>
      <span class="dialog-footer" slot="footer">
        <el-button @click="updateBanner" type="primary">确 定</el-button>
        <el-button @click="updateDialog.visible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
const BANNER_TYPE = 'BANNER_TYPE'
export default {
  data () {
    return {
      apiName: {
        uploadOss: this.apiType.common.uploadOss,
        queryBanner: this.apiType.sys.queryBanner,
        updateBanner: this.apiType.sys.updateBanner,
        deleteBanner: this.apiType.sys.deleteBanner
      },
      bannerList: [],
      fileList: [],
      preview: false,
      page: {
        total: 0,
        current: 1,
        size: 10
      },
      displayList: [
        {
          label: '显示',
          value: '1'
        },
        {
          label: '不显示',
          value: '0'
        }
      ],
      typeList: [],
      currentType: null,
      updateDialog: {
        title: '轮播图配置',
        visible: false,
        add: false,
        data: {}
      },
      currentRow: {},
      formLabelWidth: '100px',
      loading: false
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      const _this = this
      this.resultData = undefined
      this.page = {
        total: 0,
        current: 1,
        size: 20
      }
      this.loading = false
      this.queryBannerList()
      this.getDict(BANNER_TYPE, null, false).then(resp => {
        _this.typeList = Array.isArray(resp) ? resp : []
      })
    },
    getDisplay (value) {
      for (const display in this.displayList) {
        if (this.displayList[display].value === value) {
          return this.displayList[display].label
        }
      }
    },
    queryBannerList () {
      const _this = this
      _this.fetch(
        this.apiName.queryBanner,
        {type: this.currentType},
        null,
        function (resp) {
          _this.bannerList = resp.data || []
        },
        null,
        false
      )
    },
    uploadCos (file) {
      const fileObject = file.file
      let formData = new FormData()
      formData.append('file', fileObject)
      const _this = this
      const param = {
        fileName: fileObject.name,
        type: 'banner'
      }
      _this.upload(_this.apiName.uploadOss, param, formData, (resp) => {
        file.onSuccess(resp.data.data.url)
      }, null)
    },
    handleUploadSuccess (res, file) {
      this.updateDialog.data.url = res
      this.$forceUpdate()
      console.log(res, file)
    },
    addBanner () {
      this.updateDialog.visible = true
      this.updateDialog.add = true
    },
    updateBanner () {
      this.updateDialog.visible = false
      const _this = this
      console.log(this.currentRow)
      this.fetch(
        this.apiName.updateBanner,
        null,
        this.updateDialog.data,
        () => {
          _this.queryBannerList()
        }
      )
    },
    deleteBanner () {
      const _this = this
      this.$confirm('是否删除该轮播图, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.fetch(
            this.apiName.deleteBanner,
            null,
            {id: this.currentRow.id},
            () => {
              _this.queryBannerList()
            }
          )
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => {
          this.$message({
            type: 'error',
            message: err
          })
        })
    },
    clearDialog () {
      this.updateDialog.visible = false
    },
    handleCurrentChange (currentRow, oldCurrentRow) {
      this.currentRow = currentRow
    },
    setUpdateDialogData () {
      if (this.updateDialog.add) {
        this.updateDialog.data = {
          displayOrder: this.bannerList.length + 1,
          show: '0',
          type: this.currentType
        }
      } else {
        this.updateDialog.data = {
          type: this.currentType,
          ...this.currentRow
        }
      }
    },
    // 双击知识库列表
    handleDoubleSelect (row) {
      this.updateDialog.add = false
      this.updateDialog.visible = true
    },
    onChange (val) {
      this.currentType = val
    }
  }
}
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 300px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }

  .avatar {
    width: 300px;
    height: 150px;
    display: block;
  }
</style>
